<template>
  <div class="topbar">
    <div class="w topbarWrap">
      <div class="l">欢迎来到叩丁狼积分商城</div>
      <ul class="r">
        <li>
          <img class="avatar" :src="info.headImg" width="26" alt="" />
          用户名：{{ info.nickName || "滴滴" }}
        </li>
        <li>我的鸡腿：{{ info.coin || "0" }}</li>
        <li>获取鸡腿</li>
        <li>叩丁狼官网</li>
        <li class="login-btn" @click="changeIsLoginShow" v-if="!isShowCart">
          登录
        </li>
        <li
          class="login-btn"
          v-if="isShowCart"
          @click="$router.push('/user/cart')"
        >
          <img src="../assets/img/cart.png" alt="" />
          <span class="cart">购物车</span>
          <span class="num">{{ cartTotal }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import router from "@/router";
import { mapActions, mapMutations, mapState } from "vuex";
import { WeixinLoginApi } from "../utils/api";
export default {
  name: "Topbar",
  computed: {
    ...mapState("showCart", ["isShowCart"]),
    ...mapState("userInfo", ["info", "cartTotal"]),
  },
  methods: {
    ...mapMutations("loginModel", ["changeIsLoginShow"]),
    ...mapMutations("showCart", ["changeShowCart"]),
    ...mapActions("showToast", ["asyncChangeStatus"]),
    ...mapActions("userInfo", ["asyncGetUserInfo"]),
  },
  created() {
    router.onReady(async () => {
      if (this.$route.query.code) {
        let res = await WeixinLoginApi({ code: this.$route.query.code });
        // console.log(res);
        if (res.code == 0) {
          this.asyncChangeStatus({ content: "登录成功", type: "success" });
          // 扫码成功后保存token,开启购物车,获取个人信息,重新跳转到当前路径,清除路径上的code
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          this.changeShowCart(true);
          this.asyncGetUserInfo();
          router.push(this.$route.path);
          return;
        } else if (res.code == 400) {
          this.asyncChangeStatus({
            content: "二维码失效,请重新扫码",
            type: "danger",
          });
        } else if (res.code == 407) {
          this.asyncChangeStatus({
            content: "请使用手机号绑定登录微信",
            type: "info",
          });
          this.changeIsLoginShow();
          localStorage.setItem("uuid", res.uuid);
        }
      }
    });
    this.asyncGetUserInfo();
  },
  watch: {
    "$route.fullPath": {
      handler() {
        let token = localStorage.getItem("x-auth-token");
        this.changeShowCart(Boolean(token));
        this.asyncGetUserInfo();
      },
    },
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC-Light;
  .topbarWrap {
    color: #fffefe;
    display: flex;
    justify-content: space-between;
    .r {
      li {
        float: left;
        margin-left: 20px;
        text-align: center;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          display: flex;
          align-items: center;
          justify-content: center;
        }
        .cart {
          margin: 0 10px;
        }
        .num {
          display: inline-block;
          background-color: red;
          color: #fff;
          width: 20px;
          height: 20px;
          line-height: 20px;
          border-radius: 50%;
        }
      }
      .avatar {
        vertical-align: middle;
        border-radius: 50%;
      }
    }
  }
}
</style>